﻿using BroadGoalComponents.Dialogs;
using Custom.Controls;
using Custom.Model.Entity;
using Custom.Service.Repository;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Custom.Business.Sale
{
    public partial class frmSaleView : PopupForm
    {
        // 订单id
        private SaleInfo sale;
        // 商品合计金额
        private decimal total_amount = 0;

        private SaleInfoRepository saleDAL = new SaleInfoRepository();
        private SaleDetailRepository detailDAL = new SaleDetailRepository();

        public frmSaleView(long saleid)
        {
            InitializeComponent();

            this.bgDataGridView1.TopLeftHeaderCell.Value = "序号";
            this.bgDataGridView1.RowHeadersWidth = 55;
            this.bgDataGridView1.RowHeadersVisible = true;
            this.bgDataGridView1.ShowLineNo = true;

            this.sale = this.saleDAL.Get(saleid);
        }

        private void frmSaleView_Load(object sender, EventArgs e)
        {
            if (this.sale == null)
            {
                WarningDialog.Show("订单记录不存在！");
                return;
            }

            this.txtCusname.Text = this.sale.cusname;
            this.txtCardNo.Text = this.sale.cardno;
            this.txtIdentityNo.Text = this.sale.identityno;
            this.txtMobile.Text = this.sale.mobile;

            List<SaleDetail> details = this.detailDAL.Get(w => w.saleid == this.sale.id);
            if (details != null && details.Count > 0)
            {
                for (int i = 0; i < details.Count; i++)
                {
                    this.addRow(i);
                    this.bgDataGridView1.Rows[i].Cells[this.col_name.Name].Value = details[i].name;
                    this.bgDataGridView1.Rows[i].Cells[this.col_price.Name].Value = details[i].price;
                    this.bgDataGridView1.Rows[i].Cells[this.col_quantity.Name].Value = details[i].quantity;
                    this.bgDataGridView1.Rows[i].Cells[this.col_amount.Name].Value = details[i].amount;
                    decimal amount = decimal.Zero;
                    decimal.TryParse(details[i].amount.ToString(), out amount);
                    total_amount += amount;
                }
            }
            this.lbTotalAmount.Text = "合计金额：" + total_amount.ToString("0.00") + " 元";
        }

        /// <summary>
        /// 关闭窗口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// 新增1行
        /// </summary>
        /// <param name="rowIndex"></param>
        private void addRow(int rowIndex)
        {
            this.bgDataGridView1.Rows.Insert(rowIndex);
        }

    }
}
